<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>优田儿童成长中心</title>
    <link href="/static/assets/fontAwesome/css/fontawesome-all.min.css" rel="stylesheet">
    <link href="/static/assets/css/lib/themify-icons.css" rel="stylesheet">
    <link href="/static/assets/css/lib/mmc-chat.css" rel="stylesheet"/>
    <link href="/static/assets/css/lib/sidebar.css" rel="stylesheet">
    <link href="/static/assets/css/lib/bootstrap.min.css" rel="stylesheet">
    <link href="/static/assets/css/lib/nixon.css" rel="stylesheet">
    <link href="/static/assets/lib/lobipanel/css/lobipanel.min.css" rel="stylesheet">
    <link href="/static/assets/css/style.css" rel="stylesheet">
</head>

<body>
<div class="sidebar sidebar-hide-to-small sidebar-shrink sidebar-gestures">
    <div class="nano">
        <div class="nano-content">
            <ul>
                <li>
                    <a href="/home/"><i class="ti-bar-chart"></i> 统计数据</a>
                </li>
                <li class="active">
                    <a href="/teacher/"><i class="ti-user"></i> 教师管理<span
                            class="sidebar-collapse-icon ti-angle-right"></span></a>
                </li>
                <li>
                    <a href="/child/"><i class="ti-user"></i> 幼儿管理</a>
                </li>
                <li>
                    <a href="/posts/"><i class="ti-desktop"></i> 岗位管理</a>
                </li>
            </ul>
        </div>
    </div>
</div>
<div class="header">
    <div class="pull-left">
        <div class="logo">
            <a>
                <img id="logoImg" src="/static/img/yt1.png" data-logo_big="/static/img/yt1.png"
                     data-logo_small="/static/img/yt3.png" alt="Nixon"/>
            </a>
        </div>
        <div class="hamburger sidebar-toggle">
            <span class="ti-menu"></span>
        </div>
    </div>
    <div class="pull-right p-r-15">
        <ul>
            <li class="header-icon dib">
                <img class="avatar-img" src="/static/assets/images/avatar/1.jpg" alt=""/> <span
                    class="user-avatar"><span th:text="${session.username}"></span>&nbsp;&nbsp;&nbsp;<i
                    class="ti-angle-down f-s-10"></i></span>
                <div class="drop-down dropdown-profile">
                    <div class="dropdown-content-body">
                        <ul>
                            <li><a href="/exit/"><i class="ti-power-off"></i> <span>注销</span></a></li>
                        </ul>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</div>

<div class="content-wrap">
    <div class="main">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-8 p-0">
                    <div class="page-header">
                        <div class="page-title">
                            <h1>教师管理</h1>
                        </div>
                    </div>
                </div>
                <div class="col-lg-4 p-0">
                    <div class="page-header">
                        <div class="page-title">
                            <ol class="breadcrumb text-right">
                                <a href="javascript:;" class="btn-link" style="font-size: 16px;" id="back">
                                    <span class="glyphicon glyphicon-triangle-left" aria-hidden="true"></span>返回
                                </a>
                            </ol>
                        </div>
                    </div>
                </div>
            </div>
            <div class="main-content">
                <div class="row">
                    <div class="card alert">
                        <div class="card-header">
                            <h4 id="teacher-info" class="d-flex align-items-center mb-3"></h4>
                            <div class="card-header-right-icon">
                                <a href="#" class="btn btn-success m-b-10 m-l-5" id="add-salary-btn">添加教师工资</a>
                            </div>
                        </div>
                        <div class="card-body">
                            <table class="table table-responsive table-hover ">
                                <thead>
                                <tr>
                                    <th>月份</th>
                                    <th>基本工资</th>
                                    <th>奖金提成</th>
                                    <th>延点班课时费</th>
                                    <th>岗位工资</th>
                                    <th>工龄工资</th>
                                    <th>业绩提成</th>
                                    <th>应发工资</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody id="salary-table-body">
                                </tbody>
                            </table>
                            <nav>
                                <ul class="pagination" id="pagination"></ul>
                            </nav>
                        </div>
                    </div>
                </div>
            </div>
            Copyright &copy; 2024
        </div>
    </div>
</div>
<script src="/static/assets/js/lib/jquery.min.js"></script>
<script src="/static/assets/js/lib/jquery.nanoscroller.min.js"></script>
<script src="/static/assets/js/lib/sidebar.js"></script>
<script src="/static/assets/js/lib/bootstrap.min.js"></script>
<script src="/static/assets/js/lib/mmc-common.js"></script>
<script src="/static/assets/js/lib/mmc-chat.js"></script>
<script src="/static/assets/js/lib/chart-js/Chart.bundle.js"></script>
<script src="/static/assets/js/lib/chart-js/chartjs-init.js"></script>
<script src="/static/assets/js/lib/datamap/d3.min.js"></script>
<script src="/static/assets/js/lib/datamap/topojson.js"></script>
<script src="/static/assets/js/lib/datamap/datamaps.world.min.js"></script>
<script src="/static/assets/js/lib/datamap/datamap-init.js"></script>
<script src="/static/assets/lib/lobipanel/js/lobipanel.js"></script>
<script src="/static/assets/js/scripts.js"></script>
<script>
    $(document).ready(function () {
        var a = parseFloat($('#total').text());
        var b = parseFloat($('#result').text());
        $('#total').html(a + b);
        $('#lobipanel-custom-control').lobiPanel({
            reload: false,
            close: false,
            editTitle: false
        });
        $('#back').click(function () {
            location.href = '/teacher/';
        });
    });

    const teacherId = [[${t.tid}]];
    const ttype = [[${t.ttype}]];
    const pageSize = 8;

    $(function () {
        loadSalaryData(1);
        loadTeacherInfo();
    });


    function loadSalaryData(page = 1) {
        $.ajax({
            url: '/api/v1/manager/gsal/list',
            method: 'GET',
            data: {
                tid: teacherId,
                page: page,
                size: pageSize
            },
            success: function (res) {
                if (res.code === 200) {
                    renderTable(res.data.items);
                    renderPagination(res.data.total, page, pageSize);
                } else {
                    alert('加载失败：' + res.message);
                }
            },
            error: function () {
                alert('请求失败，请检查网络连接或稍后重试');
            }
        });
    }

    function renderTable(items) {
        const tbody = $('#salary-table-body');
        tbody.empty();

        items.forEach(t => {
            let overtimePay = 0;
            if (t.num < 110) overtimePay = t.num * 6;
            else if (t.num < 150) overtimePay = t.num * 7;
            else if (t.num < 190) overtimePay = t.num * 8;
            else if (t.num < 240) overtimePay = t.num * 9;
            else overtimePay = t.num * 12;

            const total = (
                t.base_sal + t.subsidy + t.edu + t.full_time + t.phone +
                t.health + t.safe + t.class_hour + t.parent +
                overtimePay + t.post_sal + t.year + t.comm
            ).toFixed(2);

            const row = `
                <tr>
                    <td>${t.month}</td>
                    <td>￥${(t.base_sal + t.subsidy + t.edu + t.full_time + t.phone).toFixed(2)}</td>
                    <td>￥${(t.health + t.safe + t.class_hour + t.parent).toFixed(2)}</td>
                    <td>￥${overtimePay.toFixed(2)}</td>
                    <td>￥${t.post_sal.toFixed(2)}</td>
                    <td>￥${t.year.toFixed(2)}</td>
                    <td>￥${t.comm.toFixed(2)}</td>
                    <td class="color-danger"><strong>￥${total}</strong></td>
                    <td>
                        <a href="/sal_mess/?sid=${t.sid}&ttype=${ttype}" class="btn btn-info m-b-10 m-l-5">详细</a>
                        <a href="/update_teachersal/?tid=${teacherId}&ttype=${ttype}&sid=${t.sid}" class="btn btn-warning m-b-10 m-l-5">编辑</a>
                        <a href="/del_teachersal/?sid=${t.sid}" class="btn btn-danger m-b-10 m-l-5">删除</a>
                    </td>
                </tr>
            `;
            tbody.append(row);
        });
    }

    function renderPagination(total, currentPage, pageSize) {
        const totalPages = Math.ceil(total / pageSize);
        const pagination = $('#pagination');
        pagination.empty();

        if (currentPage > 1) {
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="${currentPage - 1}">上一页</a></li>`);
        } else {
            pagination.append(`<li class="page-item disabled"><a class="page-link">上一页</a></li>`);
        }

        const startPage = Math.max(1, currentPage - 2);
        const endPage = Math.min(totalPages, currentPage + 2);

        if (startPage > 1) {
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="1">1</a></li>`);
            if (startPage > 2) {
                pagination.append(`<li class="page-item disabled"><a class="page-link">...</a></li>`);
            }
        }

        for (let i = startPage; i <= endPage; i++) {
            const active = i === currentPage ? 'active' : '';
            if (active === 'active') {
                pagination.append(`<li class="page-item ${active}"><a class="page-link">${i}</a></li>`);
            } else {
                pagination.append(`<li class="page-item ${active}"><a class="page-link" href="javascript:;" data-page="${i}">${i}</a></li>`);
            }
        }

        if (endPage < totalPages) {
            if (endPage < totalPages - 1) {
                pagination.append(`<li class="page-item disabled"><a class="page-link">...</a></li>`);
            }
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="${totalPages}">${totalPages}</a></li>`);
        }

        if (currentPage < totalPages) {
            pagination.append(`<li class="page-item"><a class="page-link" href="javascript:;" data-page="${currentPage + 1}">下一页</a></li>`);
        } else {
            pagination.append(`<li class="page-item disabled"><a class="page-link">下一页</a></li>`);
        }

        $('#pagination .page-link').off('click').on('click', function () {
            const page = parseInt($(this).data('page'));
            if (!isNaN(page)) {
                loadSalaryData(page);
            }
        });
    }

    $(function () {
        loadSalaryData(1);
        loadTeacherInfo();
    });

    function loadTeacherInfo() {
        $.ajax({
            url: '/api/v1/manager/teacher/detail',
            method: 'GET',
            data: {
                id: teacherId
            },
            success: function (res) {
                if (res.code === 200) {
                    const t = res.data;

                    const badge = t.ttype === 1
                        ? '<span class="badge badge-primary">托育老师</span>'
                        : '<span class="badge badge-danger">托管老师</span>';

                    const imgSrc = t.timg
                        ? `data:image/jpeg;base64,${t.timg}`
                        : '/static/img/default.png';

                    const html = `
                    <img src="${imgSrc}" width="40" height="40" class="rounded-circle mr-2 border" alt="头像">
                    <span class="color-info font-weight-bold mr-2">${t.tname}</span>
                    ${badge}
                `;

                    $('#teacher-info').html(html);

                    $('#add-salary-btn').attr('href', `/teacher_sal/add?id=${t.tid}&sid=${t.ttype}`);
                } else {
                    alert('教师信息加载失败：' + res.message);
                }
            },
            error: function () {
                alert('教师信息请求失败，请稍后再试');
            }
        });
    }
    $(document).on('click', 'a[href^="/del_teachersal/"]', function (e) {
        e.preventDefault();

        const url = $(this).attr('href');
        const urlParams = new URLSearchParams(url.split('?')[1]);
        const sid = urlParams.get("sid");

        if (!sid) {
            alert("参数错误！");
            return;
        }

        if (confirm("确认要删除该工资记录吗？")) {
            fetch(`/api/v1/manager/gsal/delete/${sid}`, {
                method: 'GET'
            })
                .then(response => response.json())
                .then(data => {
                    alert(data.message || "删除成功");
                    location.reload();
                })
                .catch(err => {
                    console.error(err);
                    alert("删除失败");
                });
        }
    });
</script>
<style>
    .copyrights {
        text-indent: -9999px;
        height: 0;
        line-height: 0;
        font-size: 0;
        overflow: hidden;
    }
</style>
<div class="copyrights">
    <h3>&copy; 版权所有 | </h3>
</div>
</body>

</html>